﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL;
using Model;
using System.Data.SqlClient;
using System.Data;

namespace BLL
{
    public class Basic_InfoBLL
    {
        Basic_InfoDAL dal = new Basic_InfoDAL();
        Loan_InfoDAL ldal = new Loan_InfoDAL();
        DictionaryDAL ddal = new DictionaryDAL();
        /// <summary>
        /// 显示项目基本信息
        /// </summary>
        /// <returns></returns>
        public List<Basic_Info> ShowBasic_Info()
        {
            return dal.Show();
        }

        /// <summary>
        /// 显示借款人信息
        /// </summary>
        /// <returns></returns>
        public List<Loan_Info> ShowLoan_Info()
        {
            return ldal.Show();
        }


        /// <summary>
        /// 显示所有字典
        /// </summary>
        /// <returns></returns>
        public List<Dictionary> ShowDictionary()
        {
            return ddal.Show();
        }
        /// <summary>
        /// 查找对应项目的借款人信息
        /// </summary>
        /// <returns></returns>
        public List<Loan_Info> ShowLoan_Info(string projectNumber)
        {
            SqlParameter[] sp1 = new SqlParameter[]{
                  new SqlParameter("projectNumber",projectNumber)
            };
            return ldal.Show(sp1);
        }


        /// <summary>
        /// 通过两表查找相应的全部信息（包含借款人）
        /// </summary>
        /// <param name="User_Id"></param>
        /// <returns></returns>
        public List<dynamic> Show(int User_Id)
        {
            var x = ShowBasic_Info(User_Id);
            var j = ShowLoan_Info();
            var zong = from a in x
                       join b in j on a.projectNumber equals b.projectNumber
                       select new
                       {
                           basic_id = a.basic_id,
                           projectNumber = a.projectNumber,
                           surplus_Years = a.surplus_Years,
                           basic_Quota = a.basic_Quota,
                           house_Located = a.house_Located,
                           house_Purpose = a.house_Purpose,
                           house_Time = a.house_Time,
                           yers_Source = a.yers_Source,
                           assessment = a.assessment,
                           presentation_Type = a.presentation_Type,
                           the_Department = a.the_Department,
                           survey = a.survey,
                           evaluation = a.evaluation,
                           basic_State = a.basic_State,
                           issue_Time = a.issue_Time,
                           survey_StartTime = a.survey_StartTime,
                           survey_EndTime = a.survey_EndTime,
                           basic_Evaluation = a.basic_Evaluation,
                           basic_ValueTime = a.basic_ValueTime,
                           basic_UnitPrice = a.basic_UnitPrice,
                           basic_Acreage = a.basic_Acreage,
                           basic_Total = a.basic_Total,
                           basic_Excl = a.basic_Excl,
                           basic_Enclosure = a.basic_Enclosure,
                           basic_CreateTime = a.basic_CreateTime,
                           User_Id = a.User_Id,
                           loan_Id = b.loan_Id,
                           loan_Name = b.loan_Name,
                           loan_IdCard = b.loan_IdCard,
                           loan_TEL = b.loan_TEL,
                           loan_Contacts = b.loan_Contacts,
                           loan_ContactsTEL = b.loan_ContactsTEL,
                           createTime = b.createTime,
                           create_People = b.create_People
                       };
            var q = zong.ToList<dynamic>();
            return q;
        }



        /// <summary>
        /// 查找最新的一个项目编号
        /// </summary>
        /// <returns></returns>
        public int Pnum()
        {
            var s = ShowBasic_Info();
            var a = s.OrderByDescending(n => n.projectNumber).FirstOrDefault();
            var num = a.projectNumber;
            return Convert.ToInt32(num);
        }



        /// <summary>
        /// 根据用户编号查找项目基本信息
        /// </summary>
        /// <returns></returns>
        public List<Basic_Info> ShowBasic_Info(int User_Id)
        {
            SqlParameter[] sp1 = new SqlParameter[]{
                  new SqlParameter("User_Id",User_Id)
            };
            return dal.Show(sp1);
        }

        /// <summary>
        /// 根据项目状态查找项目基本信息
        /// </summary>
        /// <returns></returns>
        public List<Basic_Info> ShowBasic_Info(string basic_State)
        {
            SqlParameter[] sp1 = new SqlParameter[]{
                  new SqlParameter("basic_State",basic_State)
            };
            return dal.Show(sp1);
        }

        /// <summary>
        /// 根据勘察员编号和项目状态查找项目基本信息
        /// </summary>
        /// <returns></returns>
        public List<Basic_Info> ShowBasic_Info(int survey, string basic_State)
        {
            SqlParameter[] sp1 = new SqlParameter[]{
                 new SqlParameter("survey",survey),
                  new SqlParameter("basic_State",basic_State)
            };
            return dal.Show(sp1);
        }


        /// <summary>
        /// 通过两表查找相应的全部信息（包含借款人）
        /// </summary>
        /// <param name="User_Id"></param>
        /// <returns></returns>
        public List<dynamic> Show(int survey, string basic_State)
        {
            var x = ShowBasic_Info(survey, basic_State);
            var j = ShowLoan_Info();
            var zong = from a in x
                       join b in j on a.projectNumber equals b.projectNumber
                       select new
                       {
                           basic_id = a.basic_id,
                           projectNumber = a.projectNumber,
                           surplus_Years = a.surplus_Years,
                           basic_Quota = a.basic_Quota,
                           house_Located = a.house_Located,
                           house_Purpose = a.house_Purpose,
                           house_Time = a.house_Time,
                           yers_Source = a.yers_Source,
                           assessment = a.assessment,
                           presentation_Type = a.presentation_Type,
                           the_Department = a.the_Department,
                           survey = a.survey,
                           evaluation = a.evaluation,
                           basic_State = a.basic_State,
                           issue_Time = a.issue_Time,
                           survey_StartTime = a.survey_StartTime,
                           survey_EndTime = a.survey_EndTime,
                           basic_Evaluation = a.basic_Evaluation,
                           basic_ValueTime = a.basic_ValueTime,
                           basic_UnitPrice = a.basic_UnitPrice,
                           basic_Acreage = a.basic_Acreage,
                           basic_Total = a.basic_Total,
                           basic_Excl = a.basic_Excl,
                           basic_Enclosure = a.basic_Enclosure,
                           basic_CreateTime = a.basic_CreateTime,
                           User_Id = a.User_Id,
                           loan_Id = b.loan_Id,
                           loan_Name = b.loan_Name,
                           loan_IdCard = b.loan_IdCard,
                           loan_TEL = b.loan_TEL,
                           loan_Contacts = b.loan_Contacts,
                           loan_ContactsTEL = b.loan_ContactsTEL,
                           createTime = b.createTime,
                           create_People = b.create_People
                       };
            var q = zong.ToList<dynamic>();
            return q;
        }


        /// <summary>
        /// 根据评估员编号和项目状态查找项目基本信息
        /// </summary>
        /// <returns></returns>
        public List<Basic_Info> ShowBasic_Info(string basic_State, int evaluation)
        {
            SqlParameter[] sp1 = new SqlParameter[]{
                 new SqlParameter("evaluation",evaluation),
                  new SqlParameter("basic_State",basic_State)
            };
            return dal.Show(sp1);
        }


        public List<Basic_Info> PaperBasic_Info(int PageIndex, int PageSize, out int PageCount)
        {
            PageCount = dal.Show().Count();
            return dal.Show().Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList();
        }

        /// <summary>
        /// 添加项目基本信息
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public bool AddBasic_Info(Basic_Info m)
        {
            return dal.Add(m);
        }
        /// <summary>
        /// 删除项目基本信息
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public bool DelBasic_Info(Basic_Info m)
        {
            return dal.Del(m);
        }

        /// <summary>
        /// 查询单个项目基本信息信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Basic_Info BackBasic_Info(int id)
        {
            return dal.Back(id);
        }

        /// <summary>
        /// 修改项目基本信息信息
        /// </summary>
        /// <param name="m"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool UpdBasic_Info(Basic_Info m, int id)
        {
            return dal.Update(m, id);
        }


        /// <summary>
        /// 修改项目状态为结束勘察
        /// </summary>
        /// <returns></returns>
        public bool UptBasic_Info(int id, int basic_State, string survey_EndTime)
        {
            SqlParameter[] sp1 = new SqlParameter[]{
                 new SqlParameter("survey_EndTime",survey_EndTime),
                  new SqlParameter("basic_State",basic_State)
            };
            return dal.Update1(id, sp1);
        }


        /// <summary>
        /// 评估
        /// </summary>
        /// <returns></returns>
        public bool UptpBasic_Info(Basic_Info m)
        {
            SqlParameter[] sp1 = new SqlParameter[]{
                 new SqlParameter("basic_Evaluation",m.basic_Evaluation),
                 new SqlParameter("basic_ValueTime",m.basic_ValueTime),
                 new SqlParameter("basic_UnitPrice",m.basic_UnitPrice),
                 new SqlParameter("basic_Total",m.basic_Total),
                 new SqlParameter("basic_State",m.basic_State)
            };
            return dal.Update1(m.basic_id, sp1);
        }

        /// <summary>
        /// 通过项目号修改项目状态
        /// </summary>
        /// <returns></returns>
        public bool UptSBasic_Info(Basic_Info m)
        {
            SqlParameter[] sp1 = new SqlParameter[]{
                 new SqlParameter("basic_State",m.basic_State)
            };
            return dal.Update1(m.basic_id, sp1);
        }



    }
}
